# encoding:utf-8
import time

from selenium.webdriver.chrome import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.WebDriver()
# 最大化窗口
driver.maximize_window()
# 访问url
driver.get("http://www.baidu.com")
driver.find_element(By.LINK_TEXT, "hao123").click()
time.sleep(3)
# ['6B1AE3E7F8C0B5B36B6128508857D751', '6D650D9BD783CB1BA8507146202C92F7'] 返回的窗口句柄列表
handler_list = driver.window_handles
print(handler_list)
# 00659931894C180551A9876981EAB5E0 获取当前窗口句柄
curr_handler = driver.current_window_handle
print(curr_handler)

# windows为所有窗口的列表，浏览器每打开一个窗口会在列表尾部添加一个窗口对象，所以index=-1就是切换至新打开的窗口
driver.switch_to.window(handler_list[-1])
curr_handler = driver.current_window_handle
print(curr_handler)